// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;
 
    @property(cc.Slider)
    slider: cc.Slider = null;
 
    material: cc.Material = null;
    scale: number = 1.0;


    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        this.material = this.node.getComponent(cc.Sprite).getMaterial(0);
        //设置初始缩放比例
        this.material.setProperty("scale", 1.3);
        //设置缩放显示圆的半径
        this.material.setProperty("showRadius", 20.0);
        //设置图片宽高
        this.material.setProperty("imageSize", cc.v2(this.node.width, this.node.height));

        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveCallBack, this);

    }

    private touchMoveCallBack(event: cc.Event.EventTouch){
        //当前获取到的触摸点是相对于左下角为原点的坐标，
        //需要经过一系列换算成相对于当前精灵左下角为原点的坐标。
        let pos: cc.Vec2 = event.getLocation();
        pos = pos.sub(cc.v2(cc.winSize.width/2, cc.winSize.height/2));
        pos = pos.sub(this.node.getPosition());
        pos = pos.add(cc.v2(this.node.getContentSize().width/2, this.node.getContentSize().height/2));
        let poA = new Float32Array(2);
        poA[0] = pos.x;
        poA[1] = pos.y;
        
        //修改缩放中心点
        this.material.setProperty("point", poA);
    }

    // update (dt) {}
}
